<?php
/**
 * @filename source_module.php 2013-11-11 02:10:56x
 * @author Huming Xu <huming17@126.com>
 * @version 1.0.0
 * @copyright DZF (c) 2013, Huming Xu
 */
class ctrl_course_hour extends course_hour {

    public $info_array = array();
    public $page_array = array();
    public $tree_array = array();

    static function &instance() {
        static $object;
        if (empty($object)) {
            $object = new self();
        }
        return $object;
    }
    
    public function __construct() {
        $this->_table   = 'course_hour';
        $this->_pk      = 'ch_id';
        $this->_foreign_key = '';
        $this->_keyword = 'ch_name';
        parent::__construct();
    }
    /*
     * 	取出一条信息
     */
    public function do_detail($id = '') {
        global $_G;
        //ini_set("display_errors","On");
        //error_reporting(E_ALL);
        $detail = array();
        $id = isset($_REQUEST['id']) ? $_REQUEST['id'] : '';
        $detail = $this->one_info($this->_table, $this->_pk, $id);
        //$detail['user_realname'] = $this->get_field_value_by_id('user_detail', 'user_id', $detail['user_id'], 'user_realname');
        //$detail['role_name'] = $this->get_field_value_by_id('user_role', 'role_id', $detail['role_id'], 'role_name');
        //DEBUG 输出
        $return['code']="0";
        $return['data']=$detail;
        if($_G['api']){
            //DEBUG 接口输出
            echo format_data($return, $_G['api'], $_G['mod'], $_G['action'], $_G['do'],'pc','msg_admin','admin');
        }else{
            //DEBUG 模板输出
            include template($_G['mod'].'/'.$_G['action'].'/'.$_G['do']);
        }
    }
    /*
     * 	取出一条信息
     */
    public function do_room($id = '') {
        global $_G;
        //ini_set("display_errors","On");
        //error_reporting(E_ALL);
        $detail = array();
        $id = isset($_REQUEST['id']) ? $_REQUEST['id'] : '';
        $detail = $this->one_info($this->_table, $this->_pk, $id);
        //$return['data']=$detail;
        $course_detail = $this->one_info('course_main', 'cm_id', $detail['cm_id']);
        //$detail['user_realname'] = $this->get_field_value_by_id('user_detail', 'user_id', $detail['user_id'], 'user_realname');
        //$detail['role_name'] = $this->get_field_value_by_id('user_role', 'role_id', $detail['role_id'], 'role_name');
        //获取课程进入房间URL
        $room_webservice = $_SERVER['HTTP_HOST'];//'www.jxzhjy.com';//$_SERVER['HTTP_HOST']//房间服务器域名
        $return['code']="1";
        $node_grade_id='';
        $sid = getgpc('sid', 'GP', $detail['cm_id']);//课程编号
        if(empty($sid)){
            $room_id = $detail['cm_id'];
        }
        $room_id = getgpc('room_id', 'GP', $detail['ch_id']);//课时编号
        if(empty($room_id)){
            $room_id = $detail['ch_id'];
        }
        $source_name = getgpc('source_name', 'GP', $detail['ch_name']);//课时名称
        $isp = getgpc('isp', 'GPC', 1);//运营商编号 1 电信 2 移动 3 联通
        if(empty($isp)){
            $isp = 1;
        }
        //DEBUG 配置文件
        $whiteboard_config_array=array();
        $server_node_ini_path = SITE_ROOT."config/room/config_whiteboard_".$isp.".ini";
        if(file_exists($server_node_ini_path)){
            $whiteboard_config_array = parse_ini_file($server_node_ini_path, false);
        }
        /*
        *  分配IP 老师在主 学生在从
        *  @user_type =1 学生
        *  @user_type =2 旁听 暂无
        *  @user_type =3 老师
        *  @user_type =4 助教
        *  @user_type =5 客服
        */
        if(21==$_G['user_role_id'] || 23==$_G['user_role_id']){
            $count_loop = 0;
            /*
            //DEBUG 取消动态分组+ISP识别, 变更会预先分组+ISP分组 开始
            //DEBUG 取出课程号与学生分组编号
            $group_id = DB::result_first("SELECT csg_gid FROM ".DB::table('course_student_group')." WHERE csg_chid = '".$room_id."' AND csg_uid='".$_G['user_id']."' LIMIT 1");
            //DEBUG 如果没有组的话随机给未选课人员增加一个分组
            if(empty($group_id)){
                    $service_id = rand(2,$whiteboard_config_array['SERVER_COUNT']);
                    $whiteboard_node = $whiteboard_config_array['SERVER_'.$service_id];
                    $group_id = $service_id - 1;
                    $insert_date = array(
                            'csg_chid'=>$room_id,
                            'csg_uid'=>$_G['user_id'],
                            'csg_gid'=>$group_id
                    );
                    @DB::insert('course_student_group',$insert_date,false,true);
            }
            //$service_id = $group_id + 1;//DEBUG 原服务器与组绑定注释
            */
            //DEBUG 取消动态分组+ISP识别, 变更会预先分组+ISP分组 结束
            //DEBUG 如果是按ISP取服务器节点IP
            $service_id = rand(2,$whiteboard_config_array['SERVER_COUNT']);//随机从第二个视屏节点开始取一个socket节点
            $service_socket_id = rand(2,$whiteboard_config_array['SERVER_SOCKET_COUNT']);//随机从第二个视屏节点开始取一个节点
            $whiteboard_node = $whiteboard_config_array['SERVER_'.$service_id];
            $socket_ip = $whiteboard_config_array['SERVER_SOCKET_'.$service_socket_id];
            if(empty($whiteboard_node)){
                $service_id = rand(2,$whiteboard_config_array['SERVER_COUNT']);
                $whiteboard_node = $whiteboard_config_array['SERVER_'.$service_id];
            }
            if(empty($socket_ip)){
                $service_socket_id = rand(2,$whiteboard_config_array['SERVER_SOCKET_COUNT']);
                $socket_ip = $whiteboard_config_array['SERVER_SOCKET_'.$service_socket_id];
            }
            //DEBUG 获取节点方式变更 END
            $user_type=1;//表示学生
        }elseif(22==$_G['user_role_id']){
            //DEBUG 老师和助教 默认取白板配置的第一台机器
            $whiteboard_node = $whiteboard_config_array['SERVER_1'];
            $socket_ip = $whiteboard_config_array['SERVER_SOCKET_1'];
            //DEBUG 区分 主讲 助教
            if ($_G['user_id'] == $detail['ch_teacher_id']) {
                $user_type = 3;//主讲
            }else{
                $service_id = rand(2,$whiteboard_config_array['SERVER_COUNT']);//随机从第二个视屏节点开始取一个socket节点
                $service_socket_id = rand(2,$whiteboard_config_array['SERVER_SOCKET_COUNT']);//随机从第二个视屏节点开始取一个节点
                $whiteboard_node = $whiteboard_config_array['SERVER_'.$service_id];
                $socket_ip = $whiteboard_config_array['SERVER_SOCKET_'.$service_socket_id];
                $user_type = 1;//DEBUG 其他老师进入房间
            }
        }elseif(31==$_G['user_role_id']){
            $service_id = rand(2,$whiteboard_config_array['SERVER_COUNT']);//随机从第二个视屏节点开始取一个socket节点
            $service_socket_id = rand(2,$whiteboard_config_array['SERVER_SOCKET_COUNT']);//随机从第二个视屏节点开始取一个节点
            $whiteboard_node = $whiteboard_config_array['SERVER_'.$service_id];
            $socket_ip = $whiteboard_config_array['SERVER_SOCKET_'.$service_socket_id];
            $user_type = 1;//DEBUG 其他机构人员进入房间
        }elseif(32==$_G['user_role_id']){
            $service_id = rand(2,$whiteboard_config_array['SERVER_COUNT']);//随机从第二个视屏节点开始取一个socket节点
            $service_socket_id = rand(2,$whiteboard_config_array['SERVER_SOCKET_COUNT']);//随机从第二个视屏节点开始取一个节点
            $whiteboard_node = $whiteboard_config_array['SERVER_'.$service_id];
            $socket_ip = $whiteboard_config_array['SERVER_SOCKET_'.$service_socket_id];
            $user_type = 1;//DEBUG 教研员进入房间
        }elseif(41==$_G['user_role_id']){
            $service_id = rand(2,$whiteboard_config_array['SERVER_COUNT']);//随机从第二个视屏节点开始取一个socket节点
            $service_socket_id = rand(2,$whiteboard_config_array['SERVER_SOCKET_COUNT']);//随机从第二个视屏节点开始取一个节点
            $whiteboard_node = $whiteboard_config_array['SERVER_'.$service_id];
            $socket_ip = $whiteboard_config_array['SERVER_SOCKET_'.$service_socket_id];
            $user_type = 5;//客服人员进入房间
        }elseif(3==$_G['user_role_id']){
            $service_id = rand(2,$whiteboard_config_array['SERVER_COUNT']);//随机从第二个视屏节点开始取一个socket节点
            $service_socket_id = rand(2,$whiteboard_config_array['SERVER_SOCKET_COUNT']);//随机从第二个视屏节点开始取一个节点
            $whiteboard_node = $whiteboard_config_array['SERVER_'.$service_id];
            $socket_ip = $whiteboard_config_array['SERVER_SOCKET_'.$service_socket_id];
            $user_type = 1;//超级管理员进入房间
        }else{
            $service_id = rand(2,$whiteboard_config_array['SERVER_COUNT']);//随机从第二个视屏节点开始取一个socket节点
            $service_socket_id = rand(2,$whiteboard_config_array['SERVER_SOCKET_COUNT']);//随机从第二个视屏节点开始取一个节点
            $whiteboard_node = $whiteboard_config_array['SERVER_'.$service_id];
            $socket_ip = $whiteboard_config_array['SERVER_SOCKET_'.$service_socket_id];
            $user_type = 1;//超级管理员进入房间
        }
        if(empty($user_type)){
            $user_type = 1;
        }
        //print_r($_G);
        if(!empty($whiteboard_node) && !empty($whiteboard_config_array)){
            $node_url = 'http://'.$room_webservice.'/room/index.html';
            //$swfurl = $node_url."?"."roomId=".$room_id."&userId=".$_G['user_id'];
            //DEBUG 增加扩展参数 目前绑定登录服务器IP
            //$swfurl .='&user_type='.$user_type;
            //$swfurl .='&realname='.urlencode($_G['user_realname']);
            //$swfurl .='&source_name='.urlencode($source_name);
            //$swfurl .='&main_ip='.$room_webservice;
            //$swfurl .='&courseId='.$sid;
            //$swfurl .='&socketIp='.$socket_ip;
            //$swfurl .='&videoIp='.$socket_ip;
            //$swfurl .='&staticIp='.$room_webservice;
            //$swfurl .='&sessionIp='.$room_webservice;
            //$swfurl .='&sn_group_id='.$node_grade_id;
            //DEBUG 增加地区学校
            if(empty($_G['member']['am_id_1'])){
                $am_id_1=0;
            }else{
                $am_id_1=$_G['member']['am_id_1'];
            }
             if(empty($_G['member']['am_id_2'])){
                $am_id_2=0;
            }else{
                $am_id_2=$_G['member']['am_id_2'];
            }
            if(empty($_G['member']['am_id_3'])){
                $am_id_3=0;
            }else{
                $am_id_3=$_G['member']['am_id_3'];
            }
            if(empty($_G['member']['sm_id'])){
                $sm_id=0;
            }else{
                $sm_id=$_G['member']['sm_id'];
            }
            if(empty($_G['member']['class_id'])){
                $class_id=0;
            }else{
                $class_id=$_G['member']['class_id'];
            }
            //$swfurl .='&a1='.$am_id_1;
            //$swfurl .='&a2='.$am_id_2;
            //$swfurl .='&a3='.$am_id_3;
            //$swfurl .='&s='.$sm_id;
            //$swfurl .='&c='.$class_id;
            //$swfurl .='&sname='.ext::school_name_by_uid($hour_info['teacher_id']);
            //$swfurl .='&applicantsNum='.$course_detail['cm_count'];
            //header("location:".$swfurl);
            //echo $html;
            $return['code']="0";
            $return['data']=array(
                'node_url' => $node_url,//直播间room部署URL地址
                'roomId' => $room_id,//房间编号 即课时ID
                'userId' => $_G['user_id'],//用户编号
                'user_type' => $user_type,//直播用户类型
                'realname' => urlencode($_G['user_realname']),//直播用户真实姓名 之前是URL传递因此这里加了urlencode,后续新版根据实际需要调整
                'source_name' => urlencode($source_name),//直播课时名称 之前是URL传递因此这里加了urlencode,后续新版根据实际需要调整
                'courseId' => $sid?$sid:"",//课程编号
                'socketIp' => $socket_ip,//直播房间socket服务地址(IP)
                'videoIp' => $socket_ip,//直播房间视频服务地址(IP)
                'staticIp' => $room_webservice,//直播房间静态文件服务地址(IP)
                'sessionIp' => $room_webservice,//直播房间session服务地址(IP)
                'sn_group_id' => $node_grade_id,//之前直播服务器分组，目前默认同组 可以为空
                'a1' => $am_id_1,//登录用户所属省 用于实时在线人数统计
                'a2' => $am_id_2,//登录用户所属市 用于实时在线人数统计
                'a3' => $am_id_3,//登录用户所属县 用于实时在线人数统计
                's' => $sm_id,//登录用户所属学校 用于实时在线人数统计
                'c' => $class_id,//登录用户所属班级 用于实时在线人数统计
                'applicantsNum' => $course_detail['cm_count']?$course_detail['cm_count']:"0"//选课人数 选填
            );
        }
        //DEBUG 输出
        if($_G['api']){
            //DEBUG 接口输出
            echo format_data($return, $_G['api'], $_G['mod'], $_G['action'], $_G['do'],'pc','msg_admin','admin');
        }else{
            //DEBUG 模板输出
            include template($_G['mod'].'/'.$_G['action'].'/'.$_G['do']);
        }
    }

    /*
     *  添加系统信息
    */
    public function do_add() {
        global $_G;
        //ini_set("display_errors","On");
        //error_reporting(E_ALL);
        $return['code'] = '1';
        $request_data = isset($_REQUEST[$this->_table]) && !empty($_REQUEST[$this->_table]) ? $_REQUEST[$this->_table] : '';
        if ($request_data) {
            $data=$request_data;//TODO 和数据库字段数组求差集 以便过滤冗余数据 避免SQL插入不匹配问题
            $data['create_dateline']=TIMESTAMP;
            echo $insert_id = $this->add($this->_table, $data);
            if ($insert_id) {
                $return['code'] = '0';
                $return['data']['id'] = (string)$insert_id;
            }
            if($_G['api']){
                echo format_data($return, $_G['api'], $_G['mod'], $_G['action'], $_G['do'],'pc','msg_admin','admin');die;
            }else{
                showmessage('操作成功','index.php?mod='.$_G['mod'].'&action='.$_G['action'].'&do=index');
            }
        }else{
            include template($_G['mod'].'/'.$_G['action'].'/'.$_G['do']);
        }
    }

    /*
     *  编辑系统信息
     */

    public function do_edit() {
        global $_G;
        //ini_set("display_errors","On");
        //error_reporting(E_ALL);
        $return['code'] = '1';
        $request_data = isset($_REQUEST[$this->_table]) && !empty($_REQUEST[$this->_table]) ? $_REQUEST[$this->_table] : '';
        $id = isset($_REQUEST['id']) ? $_REQUEST['id'] : '';
        if ($request_data && $id) {
            $data=$request_data;//TODO 和数据库字段数组求差集 以便过滤冗余数据 避免SQL插入不匹配问题
            $data['modify_dateline']=TIMESTAMP;
            $where = array($this->_pk => $id);
            $effect_row = $this->edit($this->_table, $data, $where);
            if ($effect_row) {
                $return['code'] = '0';
            }
            if($_G['api']){
                echo format_data($return, $_G['api'], $_G['mod'], $_G['action'], $_G['do'],'pc','msg_admin','admin');die;
            }else{
                showmessage('操作成功','index.php?mod='.$_G['mod'].'&action='.$_G['action'].'&do=index');
            }
        }else{
            include template($_G['mod'].'/'.$_G['action'].'/'.$_G['do']);
        }        
    }

    /*
     *  删除系统信息
    */
    public function do_delete() {
        global $_G;
        //ini_set("display_errors","On");
        //error_reporting(E_ALL);
        $id = isset($_REQUEST['id']) ? $_REQUEST['id'] : '';
        $return['code'] = '1';
        if ($id) {
            $where = array($this->_pk => $id);
            $effect_row = $this->delete($this->_table, $where, $limit = 1);
            if ($effect_row) {
                $return['code'] = '0';
            }
        }
        if($_G['api']){
            //DEBUG 接口输出
            echo format_data($return, $_G['api'], $_G['mod'], $_G['action'], $_G['do'],'pc','msg_admin','admin');
        }else{
            //DEBUG 模板输出
            showmessage('操作成功','index.php?mod='.$_G['mod'].'&action='.$_G['action'].'&do=index');
        }
    }

    /*
     *  系统信息列表
     */

    public function do_index() {
        global $_G;
        //ini_set("display_errors","On");
        //error_reporting(E_ALL);
        $page = max(1, intval($_GET['page']));
        $perpage = $limit = 20;
        $start = (($page - 1) * $perpage);
        $wheresql = "";
        $foreign_key = isset($_REQUEST['foreign_key']) ? $_REQUEST['foreign_key'] : '';
        if ($foreign_key) {
            $wheresql .= " AND ".$this->_foreign_key." = '" . $foreign_key . "' ";
        }
        $keyword = isset($_REQUEST['keyword']) ? $_REQUEST['keyword'] : '';
        if ($keyword) {
            $wheresql .= " AND ".$this->_keyword." LIKE '%" . $keyword . "%' ";
        }
        $orderby = " ORDER BY ".$this->_pk." DESC ";
        //DEBUG 初始化请求请求获取一页列表数据的参数
        $page_condition = array(
            'page' => $page, //int 请求页面 页码
            'limit' => $limit, //int 每页请求个数
            'perpage' => $perpage, //int 每页显示个数
            'wheresql' => $wheresql, //string //条件SQL语句 
            'orderby' => $orderby, //string 排序规则
        );
        //DEBUG 列表数据返回结构
        /*
          $page_result = array(
          //int 返回结果总数
          'total_rows' => $total_rows,
          //array 一页数据数组
          'page_data' => $page_data
          );
         */
        $page_result = $this->index($this->_table, $page_condition);
        //foreach($page_result['page_data'] AS $key => $value){
        //    $page_result['page_data'][$key]['user_realname'] = $this->get_field_value_by_id('user_detail', 'user_id', $value['user_id'], 'user_realname');
        //    $page_result['page_data'][$key]['role_name'] = $this->get_field_value_by_id('user_role', 'role_id', $value['role_id'], 'role_name');
        //}
        if ($_G['api']) {
            $return['code'] = '0';
            $return['data'] = $page_result;
            echo format_data($return, $_G['api'], $_G['mod'], $_G['action'], $_G['do'],'pc','msg_admin','admin');
        } else {
            $multipage = multi($page_result['total_rows'], $perpage, $page, "index.php?mod=".$_G['mod']."&action=".$_G['action']."&do=".$_G['do']."&keyword=".$keyword);//TODO 路由方法优化
            include template($_G['mod'].'/'.$_G['action'].'/'.$_G['do']);
        }
    }
}

?>